[kesten_processes] Rewrite Main Exercise Using JAX#301
Conversation
|
Hi @Smit-create, I have rewritten the exercise for the Kesten process in JAX. Could you have a quick look and let me know if there is anything I could change? Many thanks. |
|
Thanks for the quick update. This looks good to me! |
|
Many thanks @Smit-create. I will pass this to @jstac for review. |
|
Thanks @HumphreyYang @Smit-create Comments:
|
|
Hi @jstac, Thanks for the feedback. I have pushed an improved version based on your comments.
I wrote a comparison of using
I think pip for JAX has been included in the docker since the last update, so I did not use a pip install at the beginning. |
|
Many thanks @HumphreyYang . Some comments:
However, regarding point 3, before you proceed, please check that the big timing difference persists when you run the functions a second time. I think a lot of that difference will be in compile time, since the compiler is unrolling the for loop. |
|
CC @Smit-create regarding the conventions mentioned in my last comment. |
It's an interesting experiment to run. The timing difference is still significant (around 900 ms vs 10 s) when the jitted function is run the second time. I think this might be due to how I will proceed to include both of them as a comparison, and I think it definitely helps when explaining what |
|
Thanks @HumphreyYang , very interesting. Please go ahead. |
|
I also changed my code from using |
|
Hi @Smit-create , thanks for investigating. That's interesting. Was the acceleration in the compile time, execution time, or both? Since this is a teaching resource, and since |
I observed that the acceleration is mainly in the compile time (I tried several implementations and chose the fastest after changing some static args) See the results: %%time
w_jax_result = wealth_time_series_for_loop_jax(wdy.y_mean, ts_length, wdy, size)Output: CPU times: user 48.6 s, sys: 1.31 s, total: 50 s
Wall time: 50.5 s2nd-time results: CPU times: user 11.2 ms, sys: 0 ns, total: 11.2 ms
Wall time: 13.5 msWhile using CPU times: user 620 ms, sys: 9.76 ms, total: 630 ms
Wall time: 628 ms2nd run: CPU times: user 13.5 ms, sys: 1.03 ms, total: 14.5 ms
Wall time: 14.3 ms |
|
Thanks @Smit-create , very interesting. Compile time can be a real issue with JAX. I suggest that we include both for loop and scan code, with timing of both compile time and run time, along with some comments explaining why the timings are different. The comments should avoid specific numbers, of course, since they will keep changing as hardware / software changes. |
|
@mmcky , a friendly reminder that you are pinged above. |
|
Please let me know when this is ready for my review. |
Thanks @jstac. This is a really interesting question. I would say this is true -- jax is a tricky one though given the hardware interaction. It is installed ahead of lecture builds to set it up correctly. The one we need to use in lectures would be: it will also always need to be on its own line. @HumphreyYang could we add this at the top of the lecture with any installs that are needed (that aren't included with Perhaps we need to write a blog post about installing |
Thanks for the suggestion @mmcky. When adding the but I reckon it will raise errors for readers without CUDA support or using windows machines and will not be necessary if readers are running the code on Colab. Therefore, my concern is that the errors raised by these two lines may confuse readers instead of helping them. Maybe we can include something like: |
|
@HumphreyYang perhaps the best short term option (as you suggest) is to update the information https://python-programming.quantecon.org/jax_intro.html#jax and then update the note to include the items you noted above re: cpu and gpu options. Our lectures actually won't need to install Medium term I think we need to consider a page that discusses |
Just to make it clear as a short-run convention, we add this first and include before imports. Is this what we should use for now, if I understand it correctly? |
|
thanks @HumphreyYang just put the |
|
Hi @jstac, The GPU notes and import guide have been added. This is now ready for your review. Many thanks. |
|
@HumphreyYang rather than comments I think should go in the Might make it |
Hi @mmcky, I think the dropdown version will be better given the not is very large, but I think we need a better title for the note instead of "GPU Note" to attract readers to read this note: |
Something like "Install libraries based on GPU availability" would be nicer? |
|
One concern is that every lecture should always run without any changes on a local machine with Anaconda installed. (Otherwise we get a lot of complaints that our code is broken.) Right now both JAX installs are commented out, so this condition fails. I suggest that we do Second, in the GPU note --- which we should perhaps upgrade to a warning, we should offer two options:
For 2, I suggest that we just make this a link rather than writing it out --- since the code won't be run on our server, it's not future proof. I like @Smit-create 's suggestion for the title. We could also change it to "GPU Warning". I think it's important that all readers read that note at least once. |
|
Hi everyone, Thanks for your feedback. I have pushed a new version with some changes to GPU Warning and Please let me know if there is any change I could make. |
|
Hi @jstac, Our PRs are ready for review with GPU warnings updated. Please let us know if there is anything we need to change (CC @Smit-create) Many thanks in advance. |
|
This is very high quality. Well done @HumphreyYang (and thanks for reviewing @Smit-create ). The use of With apologies, and thanks for your patience, a few more changes to the GPU notification please: (CC @Smit-create)
Regarding the code, please try to bring it back to around 80 characters per line, at least roughly. So please modify for t in range(T):
s = s.at[:, t+1].set(jnp.where(s[:, t] < s_bar,
jnp.exp(e_random[t, :]),
jnp.exp(a_random[t, :]) * s[:, t] + jnp.exp(b_random[t, :])))I suggest giving names or symbols to |
|
This is looking good @HumphreyYang . Many thanks. Please ping me when the merge conflict is resolved and you are ready for me to merge. |
|
Hi @jstac, the conflict is resolved. Please merge when you are available. |
|
Many thanks @HumphreyYang , very good effort. Merging. |


This PR resolves #299. It is a draft to see the speed comparison on the server.